package com.harunasan.carrental.api.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harunasan.carrental.api.serve.domain.entity.CmsCar;
import com.harunasan.carrental.api.serve.dao.CmsCarMapper;
import com.harunasan.carrental.api.serve.domain.post.CmsCarPost;
import com.harunasan.carrental.api.serve.service.ICmsCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harunasan.carrental.common.exception.Assert;
import com.harunasan.carrental.common.exception.OperationException;
import com.harunasan.carrental.common.query.CommonQueryParam;
import com.harunasan.carrental.common.utils.CommonQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-02 23:26:10
 */
@Service
public class CmsCarServiceImpl extends ServiceImpl<CmsCarMapper, CmsCar> implements ICmsCarService {

    /**
     * 获取分页
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public IPage<CmsCar> pageList(CommonQueryParam commonQueryParam) {
        return this.baseMapper.pageList(
                new Page<>(commonQueryParam.getCurrent(), commonQueryParam.getSize()),
                new CommonQueryWrapper<CmsCar>(commonQueryParam).lambda()
                        .eq(CmsCar::getIsDeleted, false)
        );
    }

    /**
     * 添加车辆
     *
     * @param carPost 车辆参数
     * @return 是否成功
     */
    @Override
    public Boolean create(CmsCarPost carPost) {
        CmsCar car=new CmsCar();
        if (!this.save(car)) {
            throw new OperationException("添加车辆失败！");
        }
        return true;
    }

    /**
     * 逻辑删除
     *
     * @param id id
     * @return 是否成功
     */
    @Override
    public Boolean delete(Long id) {
        //先查询，再更新
        CmsCar car = this.getById(id);
        Assert.notNull(car, "删除失败，无此车辆！");
        car.setIsDeleted(true);
        if (!this.updateById(car)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }

    /**
     * 修改车辆信息
     * @param id id
     * @param carPost 车辆参数
     * @return 是否成功
     */
    @Override
    public Boolean update(Long id,CmsCarPost carPost) {
        //先查询，再更新
        CmsCar car = this.getById(id);
        Assert.notNull(car, "修改失败，无此车辆！");
        BeanUtils.copyProperties(carPost, car);
        if (!this.updateById(car)) {
            throw new OperationException("添加车辆信息失败！");
        }
        return true;
    }

    /**
     * 查看车辆信息
     *
     * @param id 车辆参数
     * @return 车辆信息
     */
    @Override
    public CmsCar detail(Long id) {
        CmsCar car=this.getById(id);
        Assert.notNull(car,"查询失败，无此车辆");
        return car;
    }

    /**
     * 获取列表
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public List<CmsCar> list(CommonQueryParam commonQueryParam) {
        return this.list(
                new CommonQueryWrapper<CmsCar>(commonQueryParam).lambda()
                        .eq(CmsCar::getIsDeleted, false)
        );
    }

    /**
     * 根据门店id获取车辆列表
     *
     * @param storeId xx
     * @return xx
     */
    @Override
    public List<CmsCar> getCarListByStoreId(Long storeId) {
        return this.list(
                new QueryWrapper<CmsCar>().lambda()
                        .eq(CmsCar::getStoreId, storeId)
                        .eq(CmsCar::getIsDeleted, false)
        );
    }

    /**
     * 车辆出车
     *
     * @param id 车辆id
     * @return xx
     */
    @Override
    public Boolean carLend(Long id) {
        CmsCar car = this.getById(id);
        Assert.notNull(car, "出车失败！无此车辆信息");
        Assert.isTrue(!car.getCarRentalStatus(), "出车失败，该车辆尚未归还");
        car.setCarRentalStatus(true);
        if (!this.updateById(car)) {
            throw new OperationException("出车失败！");
        }
        return true;
    }

    /**
     * 车辆归还
     *
     * @param id 车辆id
     * @return xx
     */
    @Override
    public Boolean carReturn(Long id) {
        CmsCar car = this.getById(id);
        Assert.notNull(car, "归还车辆失败！无此车辆信息");
        Assert.isTrue(car.getCarRentalStatus(), "归还车辆失败，该车辆尚未租出");
        car.setCarRentalStatus(false);
        if (!this.updateById(car)) {
            throw new OperationException("归还车辆失败！");
        }
        return true;
    }

    /**
     * 下架车辆
     *
     * @param id 车辆id
     * @return xx
     */
    @Override
    public Boolean carDisable(Long id) {
        CmsCar car = this.getById(id);
        Assert.notNull(car, "下架车辆失败！无此车辆信息");
        Assert.isTrue(car.getCarRentalStatus(), "下架车辆失败，该车辆尚未归为");
        car.setIsEnabled(false);
        if (!this.updateById(car)) {
            throw new OperationException("下架车辆失败！");
        }
        return true;
    }
}
